package com.fzl.lotterysystem.controller;

import com.fzl.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.fzl.lotterysystem.common.exception.ControllerException;
import com.fzl.lotterysystem.common.pojo.CommonResult;
import com.fzl.lotterysystem.common.utils.JacksonUtil;
import com.fzl.lotterysystem.controller.param.ShortMessageLoginParam;
import com.fzl.lotterysystem.controller.param.UserPasswordLoginParam;
import com.fzl.lotterysystem.controller.param.UserRegisterParam;
import com.fzl.lotterysystem.controller.result.BaseUserInfoResult;
import com.fzl.lotterysystem.controller.result.UserLoginResult;
import com.fzl.lotterysystem.controller.result.UserRegisterResult;
import com.fzl.lotterysystem.service.DTO.UserDTO;
import com.fzl.lotterysystem.service.DTO.UserLoginDTO;
import com.fzl.lotterysystem.service.DTO.UserRegisterDTO;
import com.fzl.lotterysystem.service.UserService;
import com.fzl.lotterysystem.service.VerificationCodeService;
import com.fzl.lotterysystem.service.enums.UserIdentityEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private VerificationCodeService verificationCodeService;

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
//    注册
    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> userRegister(
            @Validated @RequestBody UserRegisterParam param){
        //打印日志
        logger.info("userRegister UserRegisterParam:{}",JacksonUtil.writeValueAsString(param));
        //调用service
        UserRegisterDTO userRegisterDTO=userService.register(param);
        logger.info("userRegister UserRegisterParam:{}",JacksonUtil.writeValueAsString(userRegisterDTO));
        return CommonResult.success(coverToUserRegisterResult(userRegisterDTO));

    }

    @RequestMapping("/sendCode")
    public CommonResult<Boolean> sendVerificationCode(String phoneNUmber){
        logger.info("sendVerificationCode phoneNUmber:{}",phoneNUmber);
        verificationCodeService.sendVerificationCode(phoneNUmber);
        return CommonResult.success(Boolean.TRUE);
    }

    /**
     * 密码登录
     *
     * @param param
     * @return
     */
    @RequestMapping("/password/login")
    public CommonResult<UserLoginResult> UserPasswordLogin(@RequestBody @Validated UserPasswordLoginParam param){
        logger.info("UserPasswordLoginParam:{}",JacksonUtil.writeValueAsString(param));
        UserLoginDTO userLoginDTO=userService.login(param);
        return CommonResult.success(converToUserLoginResult(userLoginDTO));
    }

    /**
     * 短信验证码登录
     *
     * @param param
     * @return
     */
    @RequestMapping("/message/login")
    public CommonResult<UserLoginResult> shortMessageLogin(
            @Validated @RequestBody ShortMessageLoginParam param) {
        logger.info("shortMessageLogin ShortMessageLoginParam:{}", JacksonUtil.writeValueAsString(param));
        UserLoginDTO userLoginDTO = userService.login(param);
        return CommonResult.success(converToUserLoginResult(userLoginDTO));

    }

    /**
     *
     * @param identity
     * @return
     */
    @RequestMapping("/findUserList")
    public CommonResult<List<BaseUserInfoResult>> findBaseUserInfo (String identity){
        logger.info("findBaseUserInfo identity:{}",identity);
        List<UserDTO> userDTOList=userService.findUserInfo(UserIdentityEnum.forName(identity));
        return CommonResult.success(converToList(userDTOList));
    }

    private List<BaseUserInfoResult> converToList(List<UserDTO> userDTOList) {
        if(CollectionUtils.isEmpty(userDTOList)){
            return null;
        }
        return userDTOList.stream().map(userDTO -> {
            BaseUserInfoResult baseUserInfoResult=new BaseUserInfoResult();
            baseUserInfoResult.setUserId(userDTO.getUserId());
            baseUserInfoResult.setUserName(userDTO.getUserName());
            baseUserInfoResult.setIdentity(userDTO.getIdentity().name());
            return baseUserInfoResult;
        }).collect(Collectors.toList());
    }


    private UserLoginResult converToUserLoginResult(UserLoginDTO userLoginDTO) {
        if(userLoginDTO==null){
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
        }
        UserLoginResult userLoginResult=new UserLoginResult();
        userLoginResult.setToken(userLoginDTO.getToken());
        userLoginResult.setIdentity(userLoginDTO.getIdentity().name());
        return userLoginResult;
    }

    private UserRegisterResult coverToUserRegisterResult(UserRegisterDTO userRegisterDTO) {

        UserRegisterResult result=new UserRegisterResult();
        if(userRegisterDTO==null){
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }
        result.setUserId(userRegisterDTO.getUserId());
        return result;
    }
}
